<template>
<Select v-bind="$attrs" v-on="$listeners" :disabled="disabled" v-if="type==='Select' && $attrs.multiple" v-model="arryModel":placeholder="placeholder">
    <Option v-for="item in data" :value="item.id" :key="item.id">{{ item.name }}</Option>
</Select>
<Select v-bind="$attrs" v-on="$listeners" :disabled="disabled" v-else-if="type==='Select'" v-model="valueModel":placeholder="placeholder">
    <Option v-for="item in data" :value="item.id" :key="item.id">{{ item.name }}</Option>
</Select>
<CheckboxGroup v-bind="$attrs" v-on="$listeners" :disabled="disabled" v-else-if="type === 'Checkbox'" v-model="arryModel">
    <Checkbox :disabled="disabled" v-for="item in data" :label="item.id" :key="item.id">{{ item.name }}</Checkbox>
</CheckboxGroup>
<RadioGroup v-bind="$attrs" v-on="$listeners" :disabled="disabled" v-else-if="type === 'Radio'" v-model="valueModel">
    <Radio v-for="item in data" :label="item.id" :key="item.id">{{ item.name }}</Radio>
</RadioGroup>
</template>

<script>
export default {
    props: {
        value: {
            type: [String, Number, Array]
        },
        //编码类型
        coding: {
            type: String
        },
        //展示组件
        type: {
            type: String,
            default: "Select"
        },
        disabled: {
            type: Boolean,
            default: false
        },
        name: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            data: [],
            disabledGroup: '',
            valueModel: "",
            placeholder: "数据加载中...",
            arryModel: [],
            selectItem: {},
            allItemName: []
        };
    },
    watch: {
        value: function (val) {
            if (val == "") {
                this.arryModel = []
                this.valueModel = ""
            } else {
                if (typeof val === "object") {
                    this.arryModel = val
                } else {
                    this.valueModel = val
                    if (typeof val == "string") {
                        let nowItems = val.split(",");
                        this.arryModel = [];
                        for (let v in nowItems) {
                            this.arryModel.push(parseInt(nowItems[v]));
                        }
                    }
                }
            }
        }
    },
    methods: {
        async loadData() {
            let res = await this.$api.baseCode.getCodeByType({
                typeName: this.coding
            })
            this.data = res.rows
            this.placeholder = "请选择"

            // let defaultValue = res.rows.find(c => c.domain == "true")
            // if(defaultValue){
            //     this.valueModel = defaultValue.id
            // }
        }
    },
    mounted() {
        if (typeof this.value == "string") {
            if (this.value.indexOf(',') > -1) {
                this.arryModel = [];
                for (let val in this.value.split(",")) {
                    this.arryModel.push(parseInt(this.value[val]))
                }
            }
        }
    },
    created() {
        if (Array.isArray(this.value)) {
            for (let val in this.value) {
                this.arryModel.push(parseInt(this.value[val]))
            }
        } else {
            if (typeof this.value == "string") {
                if (this.value.indexOf(',') > -1) {
                    for (let val in this.value.split(",")) {
                        this.arryModel.push(parseInt(this.value[val]))
                    }
                } else {
                    this.arryModel.push(parseInt(this.value))
                    this.valueModel = parseInt(this.value)
                }
            } else {
                this.arryModel.push(parseInt(this.value))
                this.valueModel = parseInt(this.value)
            }
        }
        this.loadData()
    }
}
</script>

<style lang="less" scoped>

</style>
